package com.mingle.iwalk;

import java.util.Date;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
//import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class TravelerInfoCursorAdapter extends ResourceCursorAdapter {

	private int mPOILayout;
	private int mTrackLayout;
	private LayoutInflater mInflater;
	private ITrackService mTrackService;
	private Resources mRes;
	
	public TravelerInfoCursorAdapter(Context context, ITrackService service, int poiLayout, int trackLayout, Cursor c) {
		this(context, service, poiLayout, trackLayout, c, true);
		
	}

	public TravelerInfoCursorAdapter(Context context,  ITrackService service, int poiLayout, int trackLayout, Cursor c,
			boolean autoRequery) {
		super(context, 0, c, autoRequery);
		
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPOILayout = poiLayout;
		mTrackLayout = trackLayout;
		mTrackService = service;
		mRes = context.getResources();
	}

	 @Override
	 public View newView(Context context, Cursor cursor, ViewGroup parent) {
		 
		 int type = cursor.getInt(cursor.getColumnIndex(TravelerDB.TYPE));
			
		 if(type == TravelerInfo.INFO_TYPE_POI)
		 {
	        return mInflater.inflate(mPOILayout, parent, false);
		 }
		 else
		 {
			return mInflater.inflate(mTrackLayout, parent, false);
		 }
	 }
	 
	@Override
	public void bindView(View v, Context context, Cursor cursor) {
		
		int type = cursor.getInt(cursor.getColumnIndex(TravelerDB.TYPE));
		
		if(type == TravelerInfo.INFO_TYPE_POI)
		{
			TextView title = (TextView) v.findViewById(R.id.poi_item_title);
			String strTitle = cursor.getString(cursor.getColumnIndex(TravelerDB.TITLE));
			if(strTitle != null && strTitle.length() != 0)
			{
				title.setText(strTitle);
			}
			else
			{
				title.setText(R.string.no_theme);
			}
			
			TextView description = (TextView) v.findViewById(R.id.poi_item_description);
			String strDescription = cursor.getString(cursor.getColumnIndex(TravelerDB.DESCRIPTION));
			if(strDescription != null && strDescription.length() != 0)
			{
				description.setText(strDescription);
			}
			else
			{
				description.setText(R.string.no_description);
			}
			
			TextView time = (TextView) v.findViewById(R.id.poi_item_time);
			long intTime = cursor.getLong(cursor.getColumnIndex(TravelerDB.CREATED_TIME));
			String strTime = new Date(intTime).toLocaleString();
			if(strTime != null && strTime.length() != 0)
			{
				time.setText(strTime);
			}
			else
			{
				time.setText("");
			}
			
			ImageView iv = (ImageView) v.findViewById(R.id.poi_item_photo);
			byte []data = cursor.getBlob(cursor.getColumnIndex(TravelerDB.PICTURE_THUMB));
			if(data != null)
			{
				Bitmap pic = BitmapFactory.decodeByteArray(data, 0, data.length);
				iv.setImageBitmap(pic);
				iv.setBackgroundResource(R.drawable.popup);
				iv.setVisibility(View.VISIBLE);
			}
			else
			{
				iv.setVisibility(View.GONE);
			}
			
			View picInd = v.findViewById(R.id.poi_item_pic_ind);
			String picPath = cursor.getString(cursor.getColumnIndex(TravelerDB.PICTURE_PATH));
			if(picPath != null)
			{
				picInd.setVisibility(picPath.length()!=0?View.VISIBLE:View.GONE);
			}
			else
			{
				picInd.setVisibility(View.GONE);
			}
			
			View locInd = v.findViewById(R.id.poi_item_loc_ind);
			int locType = cursor.getInt(cursor.getColumnIndex(TravelerDB.LOCATION_TYPE));
			locInd.setVisibility(locType!=POIInfo.LOCATION_TYPE_UNKNOWN?View.VISIBLE:View.GONE);
			
			View voiceInd = v.findViewById(R.id.poi_item_voice_ind);
			String voicePath = cursor.getString(cursor.getColumnIndex(TravelerDB.VOICE_PATH));
			if(voicePath != null)
			{
				voiceInd.setVisibility(voicePath.length()!=0?View.VISIBLE:View.GONE);
			}
			else
			{
				voiceInd.setVisibility(View.GONE);
			}
	
			View trackInd = v.findViewById(R.id.poi_item_track_ind);
			int trackId = cursor.getInt(cursor.getColumnIndex(TravelerDB.TRACK_ID));
			trackInd.setVisibility(trackId!=TravelerInfo.INFO_INVALID_ID?View.VISIBLE:View.GONE);
	
			View uploadInd = v.findViewById(R.id.poi_item_upload_ind);
			int uploadFlag = cursor.getInt(cursor.getColumnIndex(TravelerDB.UPLOAD_FLAG));
			uploadInd.setVisibility(uploadFlag!=0?View.VISIBLE:View.GONE);
		}
		else if(type == TravelerInfo.INFO_TYPE_TRACK)
		{
			TextView title = (TextView) v.findViewById(R.id.poi_item_title);
			String strTitle = cursor.getString(cursor.getColumnIndex(TravelerDB.TITLE));
			if(strTitle != null && strTitle.length() != 0)
			{
				title.setText(strTitle);
			}
			else
			{
				title.setText(R.string.no_theme);
			}
			
			TextView description = (TextView) v.findViewById(R.id.poi_item_description);
			String strDescription = cursor.getString(cursor.getColumnIndex(TravelerDB.DESCRIPTION));
			if(strDescription != null && strDescription.length() != 0)
			{
				description.setText(strDescription);
			}
			else
			{
				description.setText(R.string.no_description);
			}
			
			TextView time = (TextView) v.findViewById(R.id.poi_item_time);
			long intTime = cursor.getLong(cursor.getColumnIndex(TravelerDB.CREATED_TIME));
			String strTime = new Date(intTime).toLocaleString();
			if(strTime != null && strTime.length() != 0)
			{
				time.setText(strTime);
			}
			else
			{
				time.setText("");
			}
			
			//TODO: 
			IndicateLayout locInd = (IndicateLayout)v.findViewById(R.id.track_item_loc_ind);
			int Id = cursor.getInt(cursor.getColumnIndex(TravelerDB.UUID));
			try {
				if(mTrackService!=null && mTrackService.getTrackServiceState() != TrackService.TRACK_SERVICE_STATUS_IDLE && mTrackService.getTrackUUID() == Id)
				{
					locInd.mIcon.setVisibility(View.GONE);
					locInd.mProgressBar.setVisibility(View.VISIBLE);
					locInd.mInfo.setText(R.string.loc_recording);
					locInd.mInfo.setVisibility(View.VISIBLE);
				}
				else
				{
					int count = cursor.getInt(cursor.getColumnIndex(TravelerDB.TRACK_COUNT));
					locInd.mIcon.setVisibility(View.VISIBLE);
					locInd.mProgressBar.setVisibility(View.GONE);
					locInd.mInfo.setText(mRes.getString(R.string.loc_count)+":"+count);
					locInd.mInfo.setVisibility(View.VISIBLE);
				}
			} catch (RemoteException e) {
				
				e.printStackTrace();
			}
			
			View uploadInd = v.findViewById(R.id.poi_item_upload_ind);
			int uploadFlag = cursor.getInt(cursor.getColumnIndex(TravelerDB.UPLOAD_FLAG));
			uploadInd.setVisibility(uploadFlag!=0?View.VISIBLE:View.GONE);
			
		}

	}

}
